use warnings;
use strict;

my $PRJDIR=$ARGV[0];
my $CURDIR=`pwd`;
my $TARGETDIR;
if (defined($PRJDIR)) {
	print "Project root path: $PRJDIR \n";
	$TARGETDIR="$PRJDIR/board/jssdk_header/include";
} else {
	chop $CURDIR;
	$PRJDIR="$CURDIR/..";
	print "Not define project root path, set $PRJDIR as root path it's get from current path! \n";
	$TARGETDIR="$CURDIR/jssdk_header/include";
}

open CFGFILE, "$PRJDIR/config";
open HEADERFILE, ">$PRJDIR/board/jssdk_mode.h";


printf HEADERFILE "#ifndef _JSSDK_MODE_H_\n";
printf HEADERFILE "#define _JSSDK_MODE_H_\n\n";

print HEADERFILE <<'EOT';
/*****************************************************
  This file is automatically generated by config2h.pl.
  Except macro MODULE_TYPE_KSLIB and MODULE_TYPE_USLIB
  Please DO NOT EDIT other items!

  when build kernel space parts you can define
  #define MODULE_TYPE_KSLIB
  when build user space parts you can define
  #define MODULE_TYPE_USLIB
******************************************************/

//#define MODULE_TYPE_KSLIB
#define MODULE_TYPE_USLIB
#ifdef MODULE_TYPE_KSLIB
    #define KERNEL_MODULE
#endif

EOT

my $kmode_cfg = 0;
my $chiptype_cfg = 0;
my $mv88e6097 = 0;
my ($attr,$val) = (undef, undef);

while (<CFGFILE>) { 

	chomp; 
	s/^\s+//;      # del space at end
	s/\s+$//;      # del space at start
	
	next if(!/=/); # skip not '='
	next if(/#/);  # skip '#' at start
	
	if(/^(.*)=(.*)$/){
		$attr = $1;
		$val = $2;
	}

	next if ($attr eq "CPU");
	next if ($attr eq "OS");
	next if ($attr eq "TOOL_PATH");
	next if ($attr eq "SYS_PATH");
	next if ($attr eq "CPU_CFLAG");

	if ($attr eq "KERNEL_MODE") {
		$kmode_cfg = 1;
		if ($val eq "TRUE") {
			printf HEADERFILE "#define KERNEL_MODE\n";
		} else {
			printf HEADERFILE "#define USER_MODE\n";
		}
	} elsif ($attr eq "CHIP_TYPE") {
		$chiptype_cfg = 1;
		if ($val eq "MARVELL_88E6097") {
			$mv88e6097 = 1;
			printf HEADERFILE "#define MARVELL_88E6097\n";
		}
  	} else {
		if ($val eq "TRUE") {
			printf HEADERFILE "#define $attr\n";
		}
	}
}

if ($kmode_cfg eq 0) {
	printf HEADERFILE "#define KERNEL_MODE\n";
}

if ($chiptype_cfg eq 0) {
	$mv88e6097 = 1;
	printf HEADERFILE "#define MARVELL_88E6097\n";
}

printf HEADERFILE "\n";
printf HEADERFILE "#endif\n";

close CFGFILE;
close HEADERFILE;

#system("cp -f $PRJDIR/board/build_mode.h $PRJDIR/include/common/build_mode.h");


#If you need't copy header files to board directory you can comment below statements
#=del
system("rm -rf $PRJDIR/board/jssdk_header");
system("mkdir  $PRJDIR/board/jssdk_header");
#system("mkdir  $TARGETDIR");

#system("cp -f $PRJDIR/board/build_mode.h $TARGETDIR/");
#system("rm -f $PRJDIR/board/build_mode.h");

#system("cp -R $PRJDIR/include/common/ $TARGETDIR/common/");

#system("mkdir $TARGETDIR/hsl");
if ($mv88e6097 eq 1) {
#	system("cp -R $PRJDIR/include/hsl/mv88e6097/ $TARGETDIR/hsl/mv88e6097/");
#	system("rm -f $TARGETDIR/hsl/mv88e6097/mv88e6097_api.h");
} 

#system("cp $PRJDIR/include/hsl/hsl.h $TARGETDIR/hsl/");
#system("cp -R $PRJDIR/include/init/ $TARGETDIR/init/");
#system("cp -R $PRJDIR/include/sal/ $TARGETDIR/sal/");
#=cut
